package com.renfei.multimediatask.camera;

import android.annotation.TargetApi;
import android.os.Build;
import android.util.Log;
import android.util.Size;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Camera2Utils {


    static class PreviewSize {
        private Size size;
        private float scale;

        public PreviewSize(Size size, float scale) {
            this.size = size;
            this.scale = scale;
        }

        public Size getSize() {
            return size;
        }

        public void setSize(Size size) {
            this.size = size;
        }

        public float getScale() {
            return scale;
        }

        public void setScale(float scale) {
            this.scale = scale;
        }
    }


    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public static Size getOptimalDiffsSize(Size[] sizeMap, int width, int height) {
        int resultWidth = 0;
        int rsultHeight = 0;

        int targetWidth;
        int targetHeight;

        int diffs = Integer.MAX_VALUE;

        List<Size> sizeList = new ArrayList<>();
        for (Size option : sizeMap) {
            targetWidth = option.getWidth();
            targetHeight = option.getHeight();

            int newDiffs = Math.abs(targetWidth - width) + Math.abs(targetHeight - height);


            if (newDiffs == 0) {

                resultWidth = targetWidth;
                rsultHeight = targetHeight;
                break;
            }

            if (diffs > newDiffs) {
                resultWidth = targetWidth;
                rsultHeight = targetHeight;
                diffs = newDiffs;
            }
        }


        return new Size(resultWidth, rsultHeight);
    }


    // 选择sizeMap中大于并且最接近width和height的size
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public static Size getOptimalSize(Size[] sizeMap, int width, int height) {
        List<Size> sizeList = new ArrayList<>();
        for (Size option : sizeMap) {
            if (width > height) {
                if (option.getWidth() > width && option.getHeight() > height) {
                    sizeList.add(option);
                }
            } else {
                if (option.getWidth() > height && option.getHeight() > width) {
                    sizeList.add(option);
                }
            }
        }
        if (sizeList.size() > 0) {
            return Collections.min(sizeList, new Comparator<Size>() {
                @Override
                public int compare(Size lhs, Size rhs) {
                    return Long.signum(lhs.getWidth() * lhs.getHeight() - rhs.getWidth() * rhs.getHeight());
                }
            });
        }
        return sizeMap[0];
    }


    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public static Size getMinSize(Size[] sizeMap) {
        List<Size> sizeList = new ArrayList<>();

        for (Size size : sizeMap) {
            sizeList.add(size);
        }

        if (sizeList.size() > 0) {
            return Collections.min(sizeList, new Comparator<Size>() {
                @Override
                public int compare(Size lhs, Size rhs) {
                    return Long.signum(lhs.getWidth() * lhs.getHeight() - rhs.getWidth() * rhs.getHeight());
                }
            });
        }
        return sizeMap[0];
    }
}
